package org.vy.dal.dao.lookup;

/**
 * @author Ravi Shankar
 *
 */

import java.util.List;

import org.vy.dal.QueryManager;

public class VyLookupDesignationDAO {

	private static final String FIND_BY_PK = "SELECT * FROM <TABLE> WHERE DESIGNATION_CODE = m_DesignationId";
	private static final String FIND_BY_PARAM = "SELECT * FROM <TABLE> WHERE 1 = 1 ";
	private static final String UPDATE = "UPDATE <TABLE> SET DESIGNATION_NAME=m_DesignationName, IS_ACTIVE=m_IsActive, LAST_UPDATED_ON=m_LastUpdatedOn, LAST_UPDATED_BY=m_LastUpdatedBy WHERE DESIGNATION_CODE = m_DesignationId";
	private static final String INSERT = "INSERT INTO <TABLE> (DESIGNATION_CODE, DESIGNATION_NAME, IS_ACTIVE, LAST_UPDATED_ON, LAST_UPDATED_BY) VALUES (m_DesignationId,m_DesignationName,m_IsActive,m_LastUpdatedOn,m_LastUpdatedBy)";
	private static final String DELETE = "UPDATE <TABLE> SET IS_ACTIVE = FALSE WHERE DESIGNATION_CODE = m_DesignationId";

	public static VyLookupDesignationDO findByPK(String key) throws Exception {
		VyLookupDesignationDO dto = new VyLookupDesignationDO();
		dto.setDesignationId(key);
		dto = (VyLookupDesignationDO) QueryManager
				.executeQuery(dto, FIND_BY_PK);
		return dto;
	}

	public static VyLookupDesignationDO insert(VyLookupDesignationDO dto) throws Exception {
		dto = (VyLookupDesignationDO) QueryManager.executeInsert(dto, INSERT);
		return dto;
	}

	public static VyLookupDesignationDO update(VyLookupDesignationDO dto) throws Exception {
		int i = QueryManager.executeUpdate(dto, UPDATE);
		if (i > 0)
			return dto;
		else
			return null;
	}

	public static VyLookupDesignationDO delete(VyLookupDesignationDO dto) throws Exception {
		int i = QueryManager.executeUpdate(dto, DELETE);
		if (i > 0)
			return dto;
		else
			return null;
	}

	@SuppressWarnings("unchecked")
	public static List<VyLookupDesignationDO> findByParam(
			VyLookupDesignationDO dto) throws Exception {
		StringBuilder sb = new StringBuilder(FIND_BY_PARAM);
		if (dto.getDesignationId() != null) {
			sb.append(" AND DESIGNATION_CODE LIKE '%"	+ dto.getDesignationId() + "%' ");
		}
		if (dto.getDesignationName() != null) {
			sb.append(" AND DESIGNATION_NAME LIKE '%"	+ dto.getDesignationName() + "%' ");
		}
		if (dto.getIsActive() != null) {
			sb.append(" AND IS_ACTIVE LIKE '%" + dto.getIsActive() + "%' ");
		}
		if (dto.getLastUpdatedOn() != null) {
			sb.append(" AND LAST_UPDATED_BY LIKE '%" + dto.getLastUpdatedOn()+ "%' ");
		}
		if (dto.getLastUpdatedBy() != null) {
			sb.append(" AND LAST_UPDATED_ON LIKE '%" + dto.getLastUpdatedBy()+ "%' ");
		}

		System.out.println(sb.toString());
		return (List<VyLookupDesignationDO>) QueryManager.executeListQuery(dto,
				sb.toString());
	}

}
